package com.fire.communication.web.web;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.fire.communication.common.base.BaseController;
import com.fire.communication.common.base.BaseService;
import com.fire.communication.model.DeviceBusiness;
import com.fire.communication.support.annotaion.Action;
import com.fire.communication.support.annotaion.At;
import com.fire.communication.support.model.RespEntity;
import com.fire.communication.support.utils.requestutil.HttpRequestUtil;
import com.fire.communication.web.service.CommunicationService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.UUID;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/7/23 15:36
 */
@Action
@RequestMapping(value = "alarmInfo")
public class CommunicationController extends BaseController {
    @Resource
    private CommunicationService communicationService;

    /**
     * 通知模块
     *
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "handle", method = RequestMethod.POST)
    public RespEntity handle(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        return communicationService.communication(jsonParam);
    }

    /**
     * @Author YangYu
     * @Description 更新短信、语音状态（云智信）
     * @Date Created in 16:29 2022/7/12
     * @Param [jsonParam]
     */
    @RequestMapping(value = "updateStateYZX", method = RequestMethod.POST)
    public RespEntity updateStateYZX(@RequestBody(required = false) JSONObject jsonParam) throws IOException {
        communicationService.updateStateYZX(jsonParam);
        return RespEntity.fail("短信状态异步更新中，请稍后查询!");
    }

    /**
     * @Author YangYu
     * @Description 阿里云短信状态更新
     * @Date Created in 14:31 2023/5/30
     * @Param [jsonArray]
     */

    @At(value = "jsonArray", method = RequestMethod.POST)
    public JSONObject testDevice(@RequestBody(required = false)JSONArray jsonArray) throws IOException {
        communicationService.updateStateAliyun(jsonArray,"1");
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("msg", "接收成功");
        return ret;
    }

    /**
     * @Author YangYu
     * @Description 阿里云语音状态更新
     * @Date Created in 14:31 2023/5/30
     * @Param [request]
     */

    @At(value = "/request", method = RequestMethod.POST)
    public JSONObject request(HttpServletRequest request) throws Exception {
        BufferedReader br = request.getReader();
        String str, wholeStr = "";
        while((str = br.readLine()) != null){
            wholeStr += str;
        }
        if (wholeStr.substring(0,1).equals("[")){
            JSONArray jsonArray = JSONArray.parseArray(wholeStr);
            communicationService.updateStateAliyun(jsonArray,"2");
        }
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("msg", "成功");
        return ret;
    }
}
